package com.example.project.easyexcel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.example.project.easyexcel.service.EasyExcelService;
import com.example.project.exception.ServiceException;
import com.example.project.utils.CellStyleUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class EasyExcelServiceImpl<T> implements EasyExcelService {
  @Autowired
  private HttpServletRequest request;
  @Autowired
  private HttpServletResponse response;

  @Override
  public void exportExcel(String name, Object obj, List<Object> list, String xlsName) {
    try {
      if (ObjectUtils.isEmpty(list)) throw new ServiceException("没有要导出的数据");
      WriteCellStyle headWriteCellStyle = new WriteCellStyle();
      //设置头居中
      headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
      //内容策略
      WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
      //设置 水平居中
      contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
      HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

      // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
      String fileName = URLEncoder.encode(name, "UTF-8");
//      response.reset();
      response.setContentType("application/x-download");
      response.setCharacterEncoding("utf-8");
      //解决不同浏览器压缩包名字含有中文时乱码的问题
//      String agent = request.getHeader("USER-AGENT");
//      try {
//        if (agent.contains("MSIE") || agent.contains("Trident")) {
//          fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
//        } else {
//
//        }
//      } catch (Exception e) {
//        e.printStackTrace();
//      }
      fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
      response.addHeader("Content-Disposition", "attachment;fileName="  + fileName + ".xlsx");
      response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
      EasyExcel.write(response.getOutputStream(), obj.getClass())
              .autoCloseStream(Boolean.FALSE)
               .registerWriteHandler(CellStyleUtils.getHorizontalCellStyleStrategy())
              .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
              .sheet(xlsName)
              .doWrite(list);
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  @Override
  public List<Object> importExcel(Object obj, MultipartFile file) {
    List<Object> objects = null;
    try {
      objects = EasyExcel.read(file.getInputStream())
              .head(obj.getClass())
              .sheet(0)
              .doReadSync();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return objects;
  }


}
